package com.yueyang.sojourn.server.controller.member;

import com.github.pagehelper.PageInfo;
import com.yueyang.sojourn.server.controller.BaseController;
import com.yueyang.sojourn.server.model.vo.SojournUserVo;
import com.yueyang.sojourn.server.service.member.MemberService;
import com.yueyang.uc.Constants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Stephen.Shi
 * @version v1
 * @summary 会员controller
 * @Copyright (c) 2019, yueyang Group All Rights Reserved.
 * @since 2020/3/29
 */
@Api(tags = "会员信息")
@RestController
@RequestMapping("/sojourn/member")
@Slf4j
public class MemberController extends BaseController {
    @Autowired
    private MemberService memberService;

    @ApiOperation(value="客户列表", notes="查询c端客户列表，包括微信小程序、公众号、h5等")
    @ApiImplicitParams({
            @ApiImplicitParam(name="regTimeStart",value="注册时间（开始） 格式:2020-02-01", paramType = "query", dataType = "string", example = "2020-02-01"),
            @ApiImplicitParam(name="regTimeEnd",value="注册时间（结束） 格式:2020-02-01", paramType = "query", dataType = "string", example = "2020-02-01"),
            @ApiImplicitParam(name = "member",value = "是否会员（0:不是 1:是） 多值使用逗号分割", dataType = "string", paramType = "query", example = "0,1"),
            @ApiImplicitParam(name = "sex",value = "性别（0: 未知 1:男 2:女）多值使用逗号分割", dataType = "string", paramType = "query", example = "0,1,2"),
            @ApiImplicitParam(name = "pageSize",value = "每页数量", dataType = "int", paramType = "query", example = "30"),
            @ApiImplicitParam(name = "pageNum",value = "页码", dataType = "int", paramType = "query", example = "1")

    })
    @GetMapping("/list")
    public PageInfo<SojournUserVo> custList(@RequestHeader(Constants.TOKEN_NAME) String token,
                                            @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam(value = "regTimeStart", required = false) Date regTimeStart,
                                            @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam(value = "regTimeEnd", required = false) Date regTimeEnd,
                                            @RequestParam(value = "member", required = false) String member,
                                            @RequestParam(value = "sex", required = false) String sex,
                                            @RequestParam(value = "keyword", required = false) String keyword,
                                            @RequestParam(value = "pageSize", required = true) Integer pageSize,
                                            @RequestParam(value = "pageNum", required = true) Integer pageNum) {

        member = member == null ? null : member.replaceAll("0", "0,2");
        List<Byte> memberValues = member == null ? new ArrayList<>() : Arrays.stream(member.split(",")).filter(s -> s.length() > 0).map(s -> Byte.valueOf(s)).collect(Collectors.toList());
        List<Byte> sexValues = sex == null ? new ArrayList<>() : Arrays.stream(sex.split(",")).filter(s -> s.length() > 0).map(s -> Byte.valueOf(s)).collect(Collectors.toList());

        return this.memberService.findCustList(keyword, regTimeStart, regTimeEnd, memberValues, sexValues, pageSize, pageNum);
    }

    @GetMapping("/count")
    public Long countAllCust() {
        return this.memberService.countCustAll();
    }

    @GetMapping("/{userId}")
    public SojournUserVo detail(@PathVariable("userId") Long userId) {
        return this.memberService.findDetail(userId);
    }
}
